/**
 * @module ChiVoxSDK
 * @author Don Li <kai.li@chivox.com>
 * @licence
 * Copyright (c) 2017 - 2020 ChiVox Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import cfg from './config.json';
import Utils from './utils';

import Html5Player from './html5/html5player';
import FlashPlayer from './flash/flashplayer';

/**
 * 初始化播放器实例的默认值。<br/>
 *
 * @name Default_ChiVoxPlayer_Options
 * @global
 * @constant
 * @property {enum=} mode - 指定播放器的模式。可以指定为ChiVoxSDK.MODE.HTML5(默认值) 或 ChiVoxSDK.MODE.FLASH。
 * @property {string} appKey - 指定播放器使用的appKey（由驰声提供）。**必填**
 * @property {string} sigurl - 指定播放器获取sig和timestamp的服务器地址（sig算法由驰声提供）。**必填**
 * @property {string=} playerid - 指定播放器渲染在当前页面的DOM节点ID。默认值: chivox-player
 * @property {callback} onInit - 播放器初始化完成后的Callback调用。格式：(code, message) => {}
 * @property {callback} onError - 播放器初始化或运行期间出错时的Callback调用。格式：(error) => {}
 */
const _default_options = {
    playerid:      'chivox-player',
    sigurl:        '',
    mode:          cfg.mode.HTML5,
    width:         100,
    height:        100,
    wmode:         'transparent',
    appKey:        '',
    onInit:        function(errno, err) {},
    onError:       null
};

/**
 * PlayerBuilder类。<br/><br/>
 *
 * 根据mode构建指定模式的播放器。
 */
class PlayerBuilder {
    /**
    * 根据mode构建指定模式的播放器。
     *
     * @param {Object} options - 初始化参数。格式参见：{@link Default_ChiVoxPlayer_Options}
     * @return {Object} - 指定模式的播放器实例。(Html5Player或FlashPlayer的实例)
     */
    static build(options) {
        var _curr_player;

        options = Utils.extend(options, _default_options, true);
        _curr_player = (options.mode == cfg.mode.HTML5) ? new Html5Player(options) : new FlashPlayer(options);

        return _curr_player;
    }
}

export default PlayerBuilder
